﻿using AutoMapper;
using GPS.DataAccessLayer;
using GPS.ENTITYS;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using PagedList;
using GPS.COMMON;
using System.IO;
using System.Web.UI;
using GPS.GPS_DAL;


namespace GPS.Controllers.GPS_Export
{
    public class GPS_BC_ToChucSatHachController : Controller
    {
        //
        // GET: /BC_ToChucSatHach/
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ActionResult Index(ExportBC_ToChucSatHach SearchModel)
        {
            //var entities = Getdata(SearchModel);
            initialCategorySearchAction();
            //ViewBag.TotalRow = entities.Count();
            var model = new List<ExportBC_ToChucSatHach>();
            //foreach (var currentEntity in entities)
            //{
            //    model.Add(Mapper.Map<ExportBC_ToChucSatHach>(currentEntity));
            //}
            //var pageIndex = SearchModel.Page ?? 1;
            //SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }


        public void initialCategorySearchAction()
        {
            ViewBag.lstDVGTVT = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
        }
        [HttpPost]
        public ActionResult ExportToWord(ExportBC_ToChucSatHach SearchModel, FormCollection form)
        {

            //string strDDLValue = form["DVGTVT_MA_Dll"].ToString();
            DataSet ds = (new GPS_BaoCaoDAO()).GetData_BC_DeNghiThiSatHach(_db.DbContext.Database.Connection.ConnectionString, "");
            ExportBC_ToChucSatHach _export = new ExportBC_ToChucSatHach();

            _export.LIST_THIMOI = COMMON.Helpers.MapDataTableToList<Export_BC_TC_ThiMoi>(ds.Tables[0]);

            string dd = "";
            foreach (var item in _export.LIST_THIMOI)
            {
                dd = item.KH_SOQDKG;
            }
            _export.KH_SOQDKG = dd;







            _export.LIST_CHUATHI = COMMON.Helpers.MapDataTableToList<Export_BC_TC_ChuaThi>(ds.Tables[1]);
            _export.LIST_VANGTHI = COMMON.Helpers.MapDataTableToList<Export_BC_TC_VangThi>(ds.Tables[2]);
            _export.LIST_THILAI = COMMON.Helpers.MapDataTableToList<Export_BC_TC_ThiLai>(ds.Tables[3]);

            //GridView gv = new GridView();
            //initialCategorySearchAction();
            //DataTable tbl = GPS.COMMON.Helpers.MapListToDataTable<ExportBC_ToChucSatHach>(Getdata(SearchModel));
            //gv.DataSource = tbl;
            //gv.DataBind();
            //ExportBC_ToChucSatHach _export = new ExportBC_ToChucSatHach();
            ////_export.LIST_THIMOI = Getdata(SearchModel).;
            ////_export.LIST_1 = ;

            byte[] contentFile = null;
            //string contentType = "application/msword";
            contentFile = GPS.ENTITYS.EXPORTWORD_BC_ToChucSatHach.EXPORT_OPENDOOR(Server.MapPath("~/Templates/GPS_Word/baocaodenghitochucthisathach.docx"), _export, "DocxGenericReport");
            string nameOutput = "EXPORT_OPENDOOR_" + DateTime.Now.ToShortDateString() + ".docx";
            return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);
        }
    }
}